/*
 * #%L
 * SwiftUICodeGenerator
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
package com.linkwithweb.products.swiftui.generator;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class LDAPTest {

	public static String ldapUri = "ldap://localhost:389";
	public static String usersContainer = "CN=Configuration,CN={D56E4159-4710-4E69-A205-591F75A4312E}";

	public static void main(String args[]) {

		/*if (args.length != 2) {
			System.out.println("Usage: test userName password");
			return;
		}*/
		String username = "";
		String password = "";

		Hashtable env = new Hashtable();
		env.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL, ldapUri);
		//env.put(Context.SECURITY_PRINCIPAL, "GA-LT000002\\ashwin");
		//env.put(Context.SECURITY_CREDENTIALS, "2011Born");
		try {
			DirContext ctx = new InitialDirContext(env);
			SearchControls ctls = new SearchControls();
			String[] attrIDs = { "cn" };
			ctls.setReturningAttributes(attrIDs);
			ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);

			NamingEnumeration answer = ctx.search(usersContainer, "(objectclass=group)", ctls);
			while (answer.hasMore()) {
				SearchResult rslt = (SearchResult) answer.next();
				Attributes attrs = rslt.getAttributes();
				System.out.println(attrs.get("cn"));
			}

			ctx.close();

		} catch (NamingException e) {
			e.printStackTrace();
		}

	}
}
